//
//  SavedSessionsTableController.m
//  TabulaImaginarum13
//
//  Created by TA2 on 7/14/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SavedSessionsTableController.h"


@implementation SavedSessionsTableController

@synthesize tableView;

- (void)viewDidLoad {
    NSArray *fileNames;
    if(TARGET_IPHONE_SIMULATOR) {
        fileNames = [[NSBundle mainBundle] pathsForResourcesOfType:nil inDirectory:nil];
    } else {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSFileManager *manager = [NSFileManager defaultManager];
        fileNames = [manager directoryContentsAtPath:documentsDirectory];
    }
	for (NSString *fileName in fileNames) {
		if ([[fileName lastPathComponent]hasSuffix:@".save"]) {
            [[LoadedImages sharedLoadedImages].savedFiles addObject:fileName];
        }
    }
	[super viewDidLoad];
	[self retain];
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	return YES;
}
#pragma mark -
#pragma mark View lifecycle

/*
 - (void)viewDidLoad {
 [super viewDidLoad];
 
 // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
 // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 }
 */

/*
 - (void)viewWillAppear:(BOOL)animated {
 [super viewWillAppear:animated];
 }
 */

 - (void)viewDidAppear:(BOOL)animated {
     [super viewDidAppear:animated];
     [sessionList reloadData];
 }
 
/*
 - (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }
 */
/*
 - (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
 }
 */

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations.
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [LoadedImages sharedLoadedImages].savedFiles.count;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
	// Configure the cell.
    cell.detailTextLabel.text = @"";
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	cell.textLabel.text = [[[LoadedImages sharedLoadedImages].savedFiles objectAtIndex:indexPath.row] stringByDeletingPathExtension];	
    return cell;
}


// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if(TARGET_IPHONE_SIMULATOR) {
        } else {
            NSError *error;
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
            NSString *documentsDirectory = [paths objectAtIndex:0];
            NSFileManager *manager = [NSFileManager defaultManager];
            [manager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:[[LoadedImages sharedLoadedImages].savedFiles objectAtIndex:indexPath.row]] error:nil];
           // NSLog(@"%@",error);
        }
        
        [[NSFileManager defaultManager] removeItemAtPath:[[LoadedImages sharedLoadedImages].savedFiles objectAtIndex:indexPath.row] error:nil];
        //[sessionList deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationFade];
        [[LoadedImages sharedLoadedImages].savedFiles removeObjectAtIndex:indexPath.row];
        [sessionList reloadData];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }   
}



/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)pressedTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[self tablePressed:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}

- (void)tableView:(UITableView *)pressedTableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
	[self tablePressed:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}

- (void)tablePressed:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    [superController loadMapWithName:[[[LoadedImages sharedLoadedImages].savedFiles objectAtIndex:indexPath.row] stringByDeletingPathExtension]];
    [sessionList deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
